//package com.zqu.ordersystem.controller;
//
//import com.zqu.ordersystem.pojo.*;
//import com.zqu.ordersystem.service.OrderDetailService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.List;
//
//@RestController  // 标识为 RESTful 控制器，处理 HTTP 请求
//@CrossOrigin  // 允许跨域请求
//public class OrderDetailController {
//
//    @Autowired  // 自动注入 OrderDetailService Bean
//    OrderDetailService orderDetailService;
//
//
//
//
//
//    // 分页获取订单详情
//    @GetMapping("/orderDetails/current/{current}/size/{size}")  // 映射 GET 请求到 getAllPage 方法
//    public Result getAllPage(@PathVariable Integer current, @PathVariable Integer size) {
//        return new Result(orderDetailService.queryPage(current, size), "查询成功", 200);  // 调用服务层方法分页查询订单详情并返回结果
//    }
//
//    // 条件分页查询订单详情
//    @PostMapping("/orderDetails/current/{current}/size/{size}")  // 映射 POST 请求到 getConditionPage 方法
//    public Result getConditionPage(@PathVariable Integer current, @PathVariable Integer size, @RequestBody OrderDetail orderDetail) {
//        return new Result(orderDetailService.queryConditionPage(orderDetail, current, size), "查询成功", 200);  // 调用服务层方法条件分页查询订单详情并返回结果
//    }
//
//
//    // 更新订单详情
//    @PutMapping("/orderDetail/{id}")  // 映射 PUT 请求到 updateOrder 方法
//    public Result updateOrder(@PathVariable Integer id, @RequestBody OrderDetail orderDetail) {
//        orderDetail.setId(id);  // 设置订单详情 ID
//        Integer upd = orderDetailService.updateOrder(orderDetail);  // 调用服务层方法更新订单详情
//        if (upd == null || upd <= 0) {  // 如果更新失败
//            return new Result(null, "修改失败", 401);  // 返回更新失败的响应
//        } else {
//            return new Result(null, "修改成功", 200);  // 返回更新成功的响应
//        }
//    }
//
//    // 删除订单详情
//    @DeleteMapping("/orderDetail/{id}")  // 映射 DELETE 请求到 deleteOrder 方法
//    public Result deleteOrder(@PathVariable Integer id) {
//        Integer del = orderDetailService.deleteOrder(id);  // 调用服务层方法删除订单详情
//        if (del == null || del <= 0) {  // 如果删除失败
//            return new Result(null, "删除失败", 401);  // 返回删除失败的响应
//        } else {
//            return new Result(null, "删除成功", 200);  // 返回删除成功的响应
//        }
//    }
//}

package ordersystem.controller;

import com.zqu.ordersystem.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;

@RestController
@CrossOrigin
public class OrderDetailController {

    @Autowired
    OrderDetailService orderDetailService;

    // 获取所有订单
    @GetMapping("/orderDetails")
    public Result getAllOrders(){
        return new Result(orderDetailService.queryAllOrder(),"查询成功",200);
    }
    // 获取单个订单
    @GetMapping("/orderDetail/{id}")
    public Result getOneOrder(@PathVariable Integer id){
        return new Result(orderDetailService.queryOneOrder(id),"查询成功",200);
    }
    //分页获取
    @GetMapping("/orderDetails/current/{current}/size/{size}")
    public Result getAllPage(@PathVariable Integer current, @PathVariable Integer size) {
        return new Result(orderDetailService.queryPage(current, size), "查询成功", 200);
    }

    // 条件分页查询
    @PostMapping("/orderDetails/current/{current}/size/{size}")
    public Result getConditionPage(@PathVariable Integer current, @PathVariable Integer size, @RequestBody OrderDetail orderDetail) {
        return new Result(orderDetailService.queryConditionPage(orderDetail, current, size), "查询成功", 200);
    }
    // 创建订单
    @PostMapping("/orderDetails")
    public Result createOrder(@RequestBody OrderDetail orderDetail){
        orderDetailService.addOrder(orderDetail);
        return new Result(null,"创建成功",200);
    }
    // 更新订单
    @PutMapping("/orderDetail/{id}")
    public Result updateOrder(@PathVariable Integer id, @RequestBody OrderDetail orderDetail){
        orderDetail.setId(id);
        Integer upd = orderDetailService.updateOrder(orderDetail);
        if(upd == null || upd <= 0){
            return new Result(null, "修改失败",401);
        } else {
            return new Result(null, "修改成功", 200);
        }
    }
    // 删除订单
    @DeleteMapping("/orderDetail/{id}")
    public Result deleteOrder(@PathVariable Integer id){
        Integer del = orderDetailService.deleteOrder(id);
        if(del == null || del <= 0){
            return new Result(null, "删除失败",401);
        } else {
            return new Result(null, "删除成功", 200);
        }
    }

}
